package com.beiding.workordersystem.orders.generalorder.controller;

import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.orders.generalorder.entity.GeneralOrder;
import com.beiding.workordersystem.orders.generalorder.query.OrderQuery;
import com.beiding.workordersystem.orders.generalorder.vo.GeneralOrderVo;
import com.beiding.workordersystem.orders.generalorder.service.GeneralOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/generalOrder/creator")
public class CreatorGeneralOrderController {

    @Autowired
    private GeneralOrderService generalOrderService;

    //新增工单
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('generalOrder_save')")
    R save(@RequestBody @Validated GeneralOrderVo vo) {
        GeneralOrder generalOrder = new GeneralOrder();
        BeanUtils.copyProperties(vo, generalOrder);
        generalOrderService.save(generalOrder);
        return R.ok(generalOrder.getId());
    }

    //创建人只能删除自己的
    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('generalOrder_delete')")
    R delete(@RequestBody String[] ids) {
        generalOrderService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('generalOrder_get')")
    R page(MPage page, OrderQuery query) {
        return R.ok(generalOrderService.page(page, query));
    }

    @PostMapping("/submit")
    R submit(@RequestBody String[] ids) {
        generalOrderService.submit(ids);
        return R.ok();
    }
}
